<?php

class Reportes extends CI_Controller {

    function Reportes() {
        parent::__construct();
		$this->load->model('ws/reportes_model');
        session_start();
        if (!$this->session->userdata('logged_in')) {
            $this->session->set_flashdata('error', 'Debe haberse logueado');
            redirect('login', 'refresh');
        }
    }
	
	public function index() {
		$data['map'] = null;
		$data['title'] = "Reportes";
        $data['username'] = $this->session->userdata('username');
        $data['name'] = $this->session->userdata('name');
        $data['acceso'] = $this->session->userdata('acceso');
		//$idUsuario = $this->session->userdata('id_contact');
        $data['main'] = "demo.php"; //Ruta del contenido del home
		
		//$data['user'] = $this->usuario_model->get_user_by_id($idUsuario);
		
        $this->load->vars($data);
		$this->load->view('user_views/dashboard_user_view');
	}
	
	public function json(){

		$whatever = '{
		"cols": [
			{"id":"","label":"Topping","pattern":"","type":"string"},
			{"id":"","label":"Slices","pattern":"","type":"number"},
			{"id":"","label":"Cost","pattern":"","type":"number"}
		  ],
		"rows": [
			{"c":[{"v":"Hawaiana","f":null},{"v":3,"f":null},{"v":40,"f":null}]},
			{"c":[{"v":"Onions","f":null},{"v":1,"f":null},{"v":30,"f":null}]},
			{"c":[{"v":"Olives","f":null},{"v":1,"f":null},{"v":60,"f":null}]},
			{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null},{"v":20,"f":null}]},
			{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null},{"v":60,"f":null}]}
		  ]
		}';
		echo $whatever;
	}
	
	public function prueba2(){		
		$array=$this->reportes_model->get_all_distritos();
		//print_r($array);
		
		$todo = '{
		"cols": [
			{"id":"","label":"Alertas","pattern":"","type":"string"},
			{"id":"","label":"Cantidad","pattern":"","type":"number"}
		  ],
		"rows": [';
		foreach ($array as $fila) {
            $todo=$todo.'{"c":[{"v":"'.$fila['distrito'].'","f":null},{"v":'.$fila['cantidad'].',"f":null}]},';
        }
		$todo = $todo.']}';
		echo $todo;
		
	}
	
	public function tipo_emergencias(){
		$fechIni=($this->input->get('fechIni')==''?"2000-01-01":$this->input->get('fechIni'));
		$fechFin=($this->input->get('fechFin')==''?"2020-01-01":$this->input->get('fechFin'));
		
		//print_r($array);
		
		$array=$this->reportes_model->get_tipo_emergencias($fechIni,$fechFin);
		//print_r($array);
		
		$todo = '{
		"cols": [
			{"id":"","label":"Alertas","pattern":"","type":"string"},
			{"id":"","label":"Cantidad","pattern":"","type":"number"}
		  ],
		"rows": [';
		foreach ($array as $fila) {
            $todo=$todo.'{"c":[{"v":"'.$fila['nombreTipoAlerta'].'","f":null},{"v":'.$fila['cantidad'].',"f":null}]},';
        }
		$todo = $todo.']}';
		echo $todo;
		
	}
	
	public function tipo_incidentes(){
		
		
		//print_r($array);
		
		$array=$this->reportes_model->get_tipo_incidentes();
		//print_r($array);
		
		$todo = '{
		"cols": [
			{"id":"","label":"Alertas","pattern":"","type":"string"},
			{"id":"","label":"Cantidad","pattern":"","type":"number"}
		  ],
		"rows": [';
		foreach ($array as $fila) {
            $todo=$todo.'{"c":[{"v":"'.$fila['nombreTipoAlerta'].'","f":null},{"v":'.$fila['cantidad'].',"f":null}]},';
        }
		$todo = $todo.']}';
		echo $todo;
		
	}
	
	public function emergencias(){		
		$fechIni=($this->input->get('fechIni')==''?"2000-01-01":$this->input->get('fechIni'));
		$fechFin=($this->input->get('fechFin')==''?"2020-01-01":$this->input->get('fechFin'));
		// echo $fechFin;
		// if ($fechIni=='') $fechaIni="2000-01-01";
		// if ($fechFin=='') $fechaFin="2020-01-01";
		$array=$this->reportes_model->get_emergencias_by_dates($fechIni,$fechFin);
		//$array=$this->reportes_model->get_all_emergencias();
		//print_r($array);
		
		$todo = '{
		"cols": [
			{"id":"","label":"Alertas","pattern":"","type":"string"},
			{"id":"","label":"Cantidad","pattern":"","type":"number"}
		  ],
		"rows": [';
		foreach ($array as $fila) {
            $todo=$todo.'{"c":[{"v":"'.$fila['distrito'].'","f":null},{"v":'.$fila['cantidad'].',"f":null}]},';
        }
		$todo = $todo.']}';
		echo $todo;
		
	}
	
	public function incidentes(){		
		$fechIni=($this->input->get('fechIni')==''?"2000-01-01":$this->input->get('fechIni'));
		$fechFin=($this->input->get('fechFin')==''?"2020-01-01":$this->input->get('fechFin'));
		
		$array=$this->reportes_model->get_all_incidentes();
		
		
		$todo = '{
		"cols": [
			{"id":"","label":"Alertas","pattern":"","type":"string"},
			{"id":"","label":"Cantidad","pattern":"","type":"number"}
		  ],
		"rows": [';
		foreach ($array as $fila) {
            $todo=$todo.'{"c":[{"v":"'.$fila['distrito'].'","f":null},{"v":'.$fila['cantidad'].',"f":null}]},';
        }
		$todo = $todo.']}';
		echo $todo;
		
	}
	
	public function prueba(){

		$this->load->dbutil();
		$this->load->helper('file');
		// get the object
		$report = $this->reportes_model->prueba();
		//pass it to db utility function
		$new_report = $this->dbutil->csv_from_result($report);
		//Now use it to write file. write_file helper function will do it
		write_file('csv_file.csv',$new_report);
		//Done	
	}	

    function logout() {
        $this->session->sess_destroy();
        $this->session->unset_userdata('logged_in');
        $this->session->set_flashdata('error', "Usted ha sido deslogueado!");
        redirect('login', 'refresh');
    }

}

?>
